package com.yiwei.ydd.activity;

import android.annotation.TargetApi;
import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.yiwei.ydd.R;
import com.yiwei.ydd.api.Api;
import com.yiwei.ydd.api.bean.LoginBean;
import com.yiwei.ydd.api.bean.ModifyCheckCodeBean;
import com.yiwei.ydd.api.bean.ModifySendBean;
import com.yiwei.ydd.base.BaseActivity;
import com.yiwei.ydd.util.Network;
import com.yiwei.ydd.util.RxLifeUtil;
import com.yiwei.ydd.util.ToastUtil;
import com.yiwei.ydd.util.UI;
import com.yiwei.ydd.util.Util;
import com.yiwei.ydd.view.V19FrameLayout;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by xsooy-pc on 17-12-8.
 */

public class ChangePhoneActivity extends BaseActivity {
    @BindView(R.id.btn_back)
    ImageView btnBack;
    @BindView(R.id.txt_title)
    TextView txtTitle;
    @BindView(R.id.txt_right)
    TextView txtRight;
    @BindView(R.id.layout_title)
    V19FrameLayout layoutTitle;
    @BindView(R.id.edit_phone)
    EditText editPhone;
    @BindView(R.id.btn_phone_close)
    ImageView btnPhoneClose;
    @BindView(R.id.edit_code)
    EditText editCode;
    @BindView(R.id.btn_send_code)
    TextView btnSendCode;
    @BindView(R.id.btn_submit)
    TextView btnSubmit;
    @BindView(R.id.btn_phone_lose)
    TextView btnPhoneLose;

    private Unbinder unbiner;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_change_phone);
        initBar();
        unbiner = ButterKnife.bind(this);
        init();
    }

    private void init() {
        txtTitle.setText("更换手机号");

    }


    public void getCodeCheck(String phone, String code) {
        if (!Network.checkNetwork(this)) {
            ToastUtil.show(this, "网络连接失败");
            return;
        }

        ModifyCheckCodeBean item = new ModifyCheckCodeBean();
        item.phone = phone;
        item.verify_code = code;
        loadingDialog.show();
        Api.api_service.getModifyCheckCode(item).compose(RxLifeUtil.checkOn(this)).
                doFinally(() -> {
                    try {
                        loadingDialog.dismiss();
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    ToastUtil.makeText(this, "验证成功");
                    Intent intent = new Intent(this, ChangePhoneSecActivity.class);
                    intent.putExtra("token", result.datas.verify_token);
                    startActivity(intent);
                    overridePendingTransition(R.anim.right_enter, R.anim.left_out);
                    finish();
                });
    }

    public void getCode(String phone) {
        if (!Network.checkNetwork(this)) {
            ToastUtil.show(this, "网络连接失败");
            return;
        }

        ModifySendBean item = new ModifySendBean();
        item.phone = phone;
        loadingDialog.show();
        Api.api_service.getModifySend(item).compose(RxLifeUtil.checkOn(this)).
                doFinally(() -> {
                    try {
                        loadingDialog.dismiss();
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    ToastUtil.makeText(this, "发送成功");
                    timer.start();
                });
    }


    private CountDownTimer timer = new CountDownTimer(60000L, 1000L) {
        public void onFinish() {
            resetBtn();
        }

        @TargetApi(16)
        public void onTick(long paramAnonymousLong) {
            btnSendCode.setEnabled(false);
            btnSendCode.setText(paramAnonymousLong / 1000L + "s后重发");
        }
    };

    @TargetApi(16)
    private void resetBtn() {
        if (this.timer != null)
            this.timer.cancel();

        //        if (this.isSend)
        //            this.btnSendCode.setText(getResources().getString(R.string.send_code_again));
        //        else
        btnSendCode.setText("获取验证码");

        btnSendCode.setEnabled(true);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (this.timer != null) {
            this.timer.cancel();
            timer = null;
        }
        unbiner.unbind();
        unbiner = null;
    }

    @OnClick({R.id.btn_back, R.id.btn_phone_close, R.id.btn_send_code, R.id.btn_submit, R.id.btn_phone_lose})
    public void onViewClicked(View view) {
        if (UI.isFastClick()) {
            return;
        }
        switch (view.getId()) {
            case R.id.btn_back:
                finish();
                break;
            case R.id.btn_phone_close:
                editPhone.setText("");
                break;
            case R.id.btn_send_code:
                if (TextUtils.isEmpty(UI.toString(editPhone))) {
                    ToastUtil.makeText(this, "手机号不能为空");
                    return;
                }
                getCode(UI.toString(editPhone));
                break;
            case R.id.btn_submit:
                if (TextUtils.isEmpty(UI.toString(editPhone))) {
                    ToastUtil.makeText(this, "手机号不能为空");
                    return;
                }
                if (TextUtils.isEmpty(UI.toString(editCode))) {
                    ToastUtil.makeText(this, "验证码不能为空");
                    return;
                }
                getCodeCheck(UI.toString(editPhone),UI.toString(editCode));
                break;
            case R.id.btn_phone_lose:
                Util.startActivity(this, ChatCenterActivity.class);
                break;
        }
    }
}
